/**
 * Created by hubo on 2016/4/20.
 */

$(document).ready(function(){

    //添加ip控件
    addIpList();

    $("#divIps").validate({
        focusCleanup : true,
        focusInvalid : false,
        onkeyup : true,
        ignore : ".ignore",// 验证隐藏域，解决资源列表以及角色名称隐藏域不予验证的问题
        rules : {
            ips : {
                required : true,
                isIps : true
            }
        },messages : {
            ips : {
                required : "必填",
                isIps : "请输入有效ip地址"
            }
        },
        submitHandler : submit
    });

});
//添加ip控件
function addIpList(){
    var MaxLength = 8;
    var divIps = $("#divIps");
    var addIps = $("#addIps");
    var len =  divIps.length;
    var tag = 1;

    //白名单列表
    var partner = getQueryString("partner");
    var data = {};
    var matcher={};
    if(partner!=""){
        matcher.partner=partner;
    }
    if(!jQuery.isEmptyObject(matcher)){
        data.matcher=JSON.stringify(matcher);
    }
    $.ajax({
        url:ctx+"/developer/query",
        type:'post',
        data:data,
        dataType:'json',
        timeout:ajaxTimeout,
        error:function(json){
            toastr.error("开启失败")
        },
        success:function(json){
            //var ipData=json.result[0].ips;
            var num = 0 ;
            var obj = json.result[0].ips;
            $("#divTable").append("<div class='ibox-title'> <h5>ips白名单列表</h5></div>");onclick="delete_(\'' + data.method +'\')"
            for(var key in obj){
                num++;
                var wIps =  obj[key].replace(/&/g,"，");
                $("#divIps").append('<div><input readonly="true"   type="text" name="ipData" value="'+wIps+'"  class="form-control ignore" /><a href="#" style="color: #1AB394;" onclick="delteWhile(\''+wIps+'\')" class="removeclass pull-right fa fa-times"  ></a><div>');
            }
        }
    });
    $(addIps).click(function(e){
        if(len<=MaxLength){
            tag++;
            $("#divIps").append("<div><input type='text'  class='form-control' name='ips' id='ip_"+ tag +"' value=''  placeholder='请输入ip白名单'/><a style='color: #1AB394;' href='#' class='removeclass pull-right fa fa-times'></a></div>");
            len++;
        }
        return false;
    });
    $("body").on("click",".removeclass",function(e){
        if(len>1){
            $(this).parent("div").remove();
            len--;
        }
        return false;
    });
}



//删除指定开发者ip白名单
function delteWhile(ips){

    $("body").on("click",".removeclass",function(e){
        $(this).parent("div").remove();
        return;
    });
    var partner = getQueryString("partner");
    $.ajax({
        url: ctx + '/developer/deleteIps',
        type: 'post',
        dataType: 'json',
        timeout: ajaxTimeout,
        traditional: true,
        data: {
            "partner": partner,
            "ips": ips
        },
        error: function (json) {
            toastr.error("删除失败");
        },
        success: function (json) {
            //重新渲染table
            if(json.returnCode=='T'){
                toastr.success(json.returnMsg);
            }else{
                toastr.error(json.returnMsg);
            }

        }

    });
}
//查询url参数
function getQueryString(name) {
    var reg = new RegExp('(^|&)' + name + '=([^&]*)(&|$)', 'i');
    var r = window.location.search.substr(1).match(reg);
    if (r != null) {
        return unescape(r[2]);
    }
    return null;
}


/**
 * 根据table对象返回跳转页码
 * @param dataTable jquery.dataTable对象
 * @param isDelete 是否为删除？删除需要考虑最后一页只有一条数据的情况
 * @returns
 */
function getPageNum(dataTable,isDelete){
    var currentPageLength = table.fnGetNodes().length;//获取当前数据的条数
    var tableSetings=dataTable.fnSettings();
    var paging_length=tableSetings._iDisplayLength;//当前每页显示多少
    var page_start=tableSetings._iDisplayStart;//当前页开始
    var page=Div(page_start,paging_length,currentPageLength,isDelete);
    return page;
};


//添加开发者ip
function submit() {
    var partner = getQueryString("partner");
    var ips=$(":input[name='ips']").map(function(){
        return $(this).val();
    }).get();
    $.ajax({
        url : ctx + '/developer/addIps?partner='+partner,
        type : 'post',
        dataType : 'json',
        timeout : ajaxTimeout,
        data : $('#divIps').serialize(),
        error : function(json) {
            toastr.error("添加失败");
        },
        success : function(json) {
            if(json.returnCode=='T'){
                toastr.success(json.returnMsg);
                setTimeout('skipWhiteList()',1000);
            }else{
                toastr.error(json.returnMsg)
            }
        }
    });
}

function skipWhiteList(){
    var partner = getQueryString("partner");
    location.href=ctx+"/send/developer/whiteAdd?partner="+partner;
}